/*
 * hal_communication.c
 *
 *  Created on: 2025 Feb 23
 *      Author: SFLY
 */
#include "hal_can_communicator.h"
#include "bsp_can.h"

void Hal_CanCom_Init(uint32_t can_id, uint32_t master_can_id,xt_hal_cantype cantype,xt_hal_can_communicator_t *pstc)
{
    pstc->can_id = can_id;
    pstc->master_can_id = master_can_id;
    Bsp_CAN_SetID(can_id);
    pstc->canType = cantype;
    pstc->isRecvFlag =0;
    if(cantype==XT_HAL_CAN_STD)
    {
        pstc->maxDatalen = 8;
//        pstc->can_set_id = Bsp_CAN_SetID;
//        pstc->can_recv_data = Bsp_CAN_RecvMessage;
//        pstc->can_send_data = Bsp_CAN_SendMessage;
    }
    else
    {
        pstc->maxDatalen = 64;
//        pstc->can_set_id = Bsp_CAN_SetID;
//        pstc->can_recv_data = Bsp_CANFD_RecvMessage;
//        pstc->can_send_data = Bsp_CANFD_SendMessage;
    }
    for(int i=0;i<64;i++)
    {
        pstc->send_candata[i] = 0;
        pstc->recv_candata[i] = 0;
    }
}


void Hal_CanCom_RecvParse(xt_hal_can_communicator_t *pstc)
{
    uint32_t recv_can_id = Bsp_CAN_RecvMessage(pstc->temp_recv_candata);
    if(recv_can_id==pstc->can_id)
    {
        //将temp_recv_candata 的值赋给 recv_candata
        pstc->isRecvFlag = 1;
        memcpy(pstc->recv_candata, pstc->temp_recv_candata, sizeof(pstc->recv_candata));
    }
    else {
        pstc->isRecvFlag = 0;
        memset(pstc->recv_candata, 0, sizeof(pstc->recv_candata));
    }
    memset(pstc->temp_recv_candata, 0, sizeof(pstc->temp_recv_candata));
}

